package com.specialtreasure.freightdepot.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.app.core.config.Constants;
import com.app.core.config.HttpApi;
import com.app.core.util.UserUtils;
import com.app.core.fragment.BaseNewFragment;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.adapter.InformationAdapter;
import com.specialtreasure.freightdepot.model.Information;
import com.specialtreasure.freightdepot.model.InformationBean;
import com.specialtreasure.freightdepot.model.InformationData;
import com.yanzhenjie.httpmodule.http.CallServer;
import com.yanzhenjie.httpmodule.http.Result;
import com.yanzhenjie.httpmodule.http.StringRequest;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.CacheMode;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/7 0007.
 * 信息单页面
 */

public class InformationHistoryFragment extends BaseNewFragment {
    public static final String TAG = "InformationHistoryFragment";
    Integer count = 10;//每页条数
    Integer page = 1;//页数
    String token;
    private ListView lv_moto_cade;
    private PtrClassicFrameLayout ptr_layout;

    Boolean hasMode;
    List<InformationBean> informations=new ArrayList<InformationBean>();
    InformationAdapter informationAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(mContext, R.layout.frag_information, null);
        initView(view);
        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setData();
    }

    private void setData() {

        //初始化自动刷新
        ptr_layout.postDelayed(new Runnable() {
            @Override
            public void run() {
                ptr_layout.autoRefresh(true);
            }
        }, 150);

        //下拉刷新
        ptr_layout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                ptr_layout.setLoadMoreEnable(true);
                page=1;
                informations.clear();
                getData();

            }
        });

        //自动加载更多
        ptr_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                if (!hasMode){
                    page++;
                    getData();
                }else {
                    Toast.makeText(mContext, "没有更多数据了", Toast.LENGTH_SHORT).show();
                    ptr_layout.setLoadMoreEnable(false);
                }
            }
        });
    }


    /**
     * 网络请求
     */
    private void getData() {
        token= UserUtils.getToken();
        String url = Constants.HTTP_URL + HttpApi.selectitemhistory;
        StringRequest request = new StringRequest(url, RequestMethod.POST);
        request.add("token", token);
        request.add("count", count);
        request.add("page", page);

        request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);

        CallServer.getInstance().request(Constants.ZERO, request, new SimpleResponseListener<Result<String>>() {
            @Override
            public void onSucceed(int what, Response<Result<String>> response) {
                super.onSucceed(what, response);
                ptr_layout.refreshComplete();
                ptr_layout.loadMoreComplete(true);

                String result = response.get().getResult();
                Logger.e("请求成功：" + result);

                Information information = JSON.parseObject(result,Information.class);

                if (information != null) {
                    Integer status = information.getStatus();
                    String msg = information.getMsg();
                    //判断状态是
                    if (status != null && status == 0) {
                        InformationData informationData=information.getData();
                        if (informationData!=null){
                            hasMode=informationData.getHasMore();
                            //适配数据
                            List<InformationBean> motorCadetList=informationData.getResultList();
                            if (motorCadetList!=null&&motorCadetList.size()>0){
                                informations.addAll(motorCadetList);
                            }

                            if (informationAdapter==null){
                                informationAdapter=new InformationAdapter(getActivity());
                                informationAdapter.setInformations(informations);
                                lv_moto_cade.setAdapter(informationAdapter);
                            }else {
                                informationAdapter.setInformations(informations);
                                informationAdapter.notifyDataSetChanged();
                            }
                        }

                    } else {
                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                    }
                }


            }
        });
    }

    @Override
    protected String getFragmentName() {
        return TAG;
    }



    private void initView(View view) {
        lv_moto_cade = (ListView) view.findViewById(R.id.lv_moto_cade);
        ptr_layout = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_layout);
    }
}
